;(function (w, d) {
  const root = d.documentElement

  function getFontSizeScaleRate() {
    try {
      if (window.innerWidth >= 750) {
        // 以768px为界限，超过该宽度不放大字体
        return 1
      }

      const $dom = document.createElement('div')
      $dom.innerHTML = 'Test' // 设置内容以计算字体大小
      $dom.setAttribute('style', 'font-size:12px;')
      document.body.appendChild($dom)
      const scaledFontSize = parseFloat(window.getComputedStyle($dom).getPropertyValue('font-size'))
      document.body.removeChild($dom)
      return Math.min(12 / scaledFontSize, 1) // 限制最大放大比例为1
    } catch (error) {
      return 1
    }
  }

  function setRootFontSize() {
    let screenWidth = root.clientWidth
    screenWidth = Math.min(1320, Math.max(240, screenWidth)) // 限制最小和最大屏幕宽度
    const baseFontSize = screenWidth / 18.75
    root.style.fontSize = baseFontSize * getFontSizeScaleRate() + 'px'
  }

  function setBodyFontSize() {
    if (d.body) {
      d.body.style.fontSize = 16 * getFontSizeScaleRate() + 'px'
    } else {
      d.addEventListener('DOMContentLoaded', setBodyFontSize)
    }
  }

  setBodyFontSize()
  setRootFontSize()
  w.addEventListener('resize', setRootFontSize)
  w.addEventListener('pageshow', setRootFontSize)
})(window, document)
